<?php
/**
 * 预约管理
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class appCon extends rootCon
{
	/**
	 * 预约核销
	 */
    public function verifyAction(){
        $uid = mylogin::getUid(true);
        $app = new CourseAppointment();
        $app->load();
        if(r('isDebug')){
            $app->courseAppointmentId = 1;
        }else{
            $app->uid = $uid;
        }
        $find = $app->find();
        if(!$find){
            $this->j(-1,"没有找到预约记录");
        }

        $app->status = STATUS_HEXIAO;
        $app->save();

        $this->j(0,"");
    }

    //老师查看预约详情
    public function teacherViewAction(){
        $uid = mylogin::getUid(true);

        $app = new CourseAppointment();
        $app->load();
        if(r('isDebug')){
            $app->courseAppointmentId = 1;
        }
        $find = $app->find();
        if(!$find && !r('isDebug')){
            $this->j(-1,"您没有权限查看");
        }

        //找到老师所有的课程
        $teacher = new Teacher();
        $teacher->find(['uid'=>$uid]);

        $courseTeacher = new CourseTeacher();
        $courseTeacher->teacherId = $teacher->teacherId;
        $courseTeacher->status = STATUS_PUBLISH;
        $courseTeacher->courseId = $app->courseId;
        $find = $courseTeacher->find();
        if(!$find && !r('isDebug'))  $this->j(-1,"您没有权限查看!");

        $course = new Course();
        $course->find($app->courseId);

        $app->arr['courseName'] = $course->courseName;
        $school = new School();
        $school->find($course->schoolId);
        $app->arr['addr'] = $school->addr;

        $app->hide();
        $this->j(0,"",$app->arr);
    }

    //显示签到二维码
    public function  qrAction(){
        $uid = mylogin::getUid(true);

        $app = new CourseAppointment();
        $app->load();
        if(r('isDebug')){
            $app->courseAppointmentId = 1;
        }
        $find = $app->find();
        if(!$find && !r('isDebug')){
            $this->j(-1,"您没有权限查看");
        }

        //找到老师所有的课程
        $teacher = new Teacher();
        $teacher->find(['uid'=>$uid]);

        $courseTeacher = new CourseTeacher();
        $courseTeacher->teacherId = $teacher->teacherId;
        $courseTeacher->status = STATUS_PUBLISH;
        $courseTeacher->courseId = $app->courseId;
        $find = $courseTeacher->find();
        if(!$find && !r('isDebug'))  $this->j(-1,"您没有权限查看!");

        $course = new Course();
        $course->find($app->courseId);

        $app->arr['courseName'] = $course->courseName;
        $school = new School();
        $school->find($course->schoolId);
        $app->arr['addr'] = $school->addr;

        $app->hide();
        $this->j(0,"",$app->arr);
    }

    //列出用户的预约记录
    public function listStudentAction(){
        $uid = mylogin::getUid(true);
        $status = r_int('status');

        $app = new CourseAppointment();
        $datetime = date("Y-m-d H:i:s",time()-3600*2);
        $sql = "SELECT * FROM ".$app->table()." WHERE uid=$uid AND status=".STATUS_PUBLISH."  ";
        $all = $app->query($sql);
        foreach($all as $item){
            if($item['dateAndTime'] <= $datetime){
                $tempApp = new CourseAppointment();
                $tempApp->courseAppointmentId = $item['courseAppointmentId'];
                $tempApp->status = STATUS_ORDER_EXPIRE;
                $tempApp->save();
            }
        }

        $sql = "SELECT * FROM ".$app->table()." WHERE uid=$uid AND status=".$status." ";
        $all = $app->query($sql);
        $i = 0;
        $rect = [];
        foreach($all as $item){
            $course = new Course();
            $course->find($item['courseId']);
            $item['courseName'] = $course->courseName;

            $school = new School();
            $school->find($item['schoolId']);
            $item['schoolName'] = $school->schoolName;

            $rect[] = $item;
            $i++;
        }

        $this->j(0, "list",$rect);
    }

	
	/**
	 * 机构和教师的预约列表
	 */
    public function listAction() {
        $uid = mylogin::getUid(true);
        $status = r_int('status');

        $courseIds = [-1];
        if(r('isDebug')){
            $courseIds[] =  5;
            $courseIds[] =  7;
        }



        //机构管理员，找到所有的课程
        if(r_int('schoolAdmin')){
            $schoolAdmin = new SchoolAdmin();
            $schoolAdmin->uid = $uid;
            $schoolAdmin->status = STATUS_PUBLISH;
            $find = $schoolAdmin->find();
            if(!$find){
                $this->j(-1,'您不是通过审核的机构管理员');
            }
            $schoolId = $schoolAdmin->schoolId;
            $course = new Course();
            $course->status = STATUS_PUBLISH;
            $course->schoolId = $schoolId;
            $all = $course->all();
        }else{//教师，找到老师对应的课程
            $teacher = new Teacher();
            $teacher->find(['uid'=>$uid]);

            $courseTeacher = new CourseTeacher();
            $courseTeacher->teacherId = $teacher->teacherId;
            $courseTeacher->status = STATUS_PUBLISH;
            $all = $courseTeacher->all();
        }
        foreach($all as $item){
            if($item['courseId']) $courseIds[] = $item['courseId'];
        }

        $app = new CourseAppointment();
        $courseIdStr = join(",",$courseIds);
        $datetime = date("Y-m-d H:i:s",time()-3600*2);
        $sql = "SELECT * FROM ".$app->table()." WHERE courseId IN ($courseIdStr) AND status=".STATUS_PUBLISH."  ";
        $all = $app->query($sql);
        foreach($all as $item){
            if($item['dateAndTime'] <= $datetime){
                $tempApp = new CourseAppointment();
                $tempApp->courseAppointmentId = $item['courseAppointmentId'];
                $tempApp->status = STATUS_ORDER_EXPIRE;
                $tempApp->save();
            }
        }


        $sql = "SELECT * FROM ".$app->table()." WHERE courseId IN ($courseIdStr) AND status=".$status." ORDER BY courseAppointmentId DESC";
        $all = $app->query($sql);
        $i = 0;
        $rect = [];
        foreach($all as $item){
            $course = new Course();
            $course->find($item['courseId']);
            $item['courseName'] = $course->courseName;

            $school = new School();
            $school->find($item['schoolId']);
            $item['schoolName'] = $school->schoolName;
            $rect[] = $item;
            $i++;
        }

        $info = '';
        if(IS_DEBUG) $info = $sql;

        $this->j(0, $info,$rect);
    }


}



